package com.muyu.vehicle.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.domain.es.InfoEs;
import com.muyu.vehicle.process.SynchronizeVehicleFenceThread;
import com.muyu.vehicle.process.ThreadPool;
import com.muyu.vehicle.utils.ElkLogs;
import com.muyu.vehicle.utils.PageInfo;
import com.vehicle.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.vehicle.common.log.annotation.Log;
import com.vehicle.common.log.enums.BusinessType;
import com.vehicle.common.security.annotation.PreAuthorize;
import com.muyu.vehicle.domain.VehicleInfo;
import com.muyu.vehicle.service.IVehicleInfoService;
import com.vehicle.common.core.web.controller.BaseController;
import com.vehicle.common.core.web.domain.AjaxResult;
import com.vehicle.common.core.utils.poi.ExcelUtil;
import com.vehicle.common.core.web.page.TableDataInfo;

/**
 * 车辆信息Controller
 *
 * @author muyu
 * @date 2021-09-04
 */
@RestController
@RequestMapping("/info")

public class VehicleInfoController extends BaseController
{
    @Autowired
    private IVehicleInfoService vehicleInfoService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 查询车辆信息列表
     */
    @PreAuthorize(hasPermi = "vehicle:info:list")
    @GetMapping("/list")
    @ElkLogs
    public TableDataInfo list(VehicleInfo vehicleInfo)
    {
        startPage();
        List<VehicleInfo> list = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @PreAuthorize(hasPermi = "vehicle:info:export")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VehicleInfo vehicleInfo) throws IOException
    {
        List<VehicleInfo> list = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        ExcelUtil<VehicleInfo> util = new ExcelUtil<VehicleInfo>(VehicleInfo.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize(hasPermi = "vehicle:info:query")
    @GetMapping(value = "/{id}")
    @ElkLogs
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(vehicleInfoService.selectVehicleInfoById(id));
    }

    /**
     * 新增车辆信息
     */
    @PreAuthorize(hasPermi = "vehicle:info:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody VehicleInfo vehicleInfo)
    {
        return toAjax(vehicleInfoService.insertVehicleInfo(vehicleInfo));

    }

    /**
     * 修改车辆信息
     */
    @PreAuthorize(hasPermi = "vehicle:info:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody VehicleInfo vehicleInfo)
    {
        try {
            return toAjax(vehicleInfoService.updateVehicleInfo(vehicleInfo));
        }finally {
            ThreadPool.exeThread(new SynchronizeVehicleFenceThread(redisTemplate,vehicleInfoService,vehicleInfo.getVehicleVin()));
        }

    }

    /**
     * 删除车辆信息
     */
    @PreAuthorize(hasPermi = "vehicle:info:remove")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(vehicleInfoService.deleteVehicleInfoByIds(ids));
    }

    /**
     * 车辆实时数据
     */
    @PreAuthorize(hasPermi = "vehicle:info:remove")
    @Log(title = "车辆实时数据", businessType = BusinessType.DELETE)
	@GetMapping("/realTimeLocus/{vin}")
    public AjaxResult realTimeLocus(@PathVariable(value = "vin") String vin)
    {
        logger.info("VIN：{} 需要进行分流",vin);
        redisTemplate.opsForSet().add(Config.VEHICLE_IS_MSG , vin);
        return AjaxResult.success();
    }
    /**
     * 车辆实时数据
     */
    @PreAuthorize(hasPermi = "vehicle:info:remove")
    @Log(title = "车辆实时数据取消查看", businessType = BusinessType.DELETE)
	@DeleteMapping("/realTimeLocus/{vin}")
    public AjaxResult realTimeLocusDel(@PathVariable(value = "vin") String vin)
    {
        logger.info("VIN：{} 取消分流",vin);
        redisTemplate.opsForSet().remove(Config.VEHICLE_IS_MSG , vin);
        return AjaxResult.success();
    }
    /**
     * 车辆实时数据
     */
    @PreAuthorize(hasPermi = "vehicle:info:remove")
	@GetMapping("/data/{vin}")
    public AjaxResult vehicleDataByVin(@PathVariable(value = "vin") String vin)
    {
        String vehicleData = redisTemplate.opsForList().rightPop(Config.VEHICLE_IS_MSG + ":" + vin);
        return AjaxResult.success(JSONObject.parseObject(vehicleData));
    }

    @GetMapping("/indexData")
    public AjaxResult getIndexVehicleData(){

        return AjaxResult.success(vehicleInfoService.getIndexVehicleData());
    }

    /**
     * 车辆信息分词高亮查询
     */
    @PostMapping("/getEsList")
    @ElkLogs
    public R esList(@RequestBody PageInfo pageInfo){
        // 自动绑定请求参数到pageInfo
        return vehicleInfoService.esList(pageInfo);
    }
}
